define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'common/cmdlog/index' + location.search,
                    add_url: 'common/cmdlog/add',
                    edit_url: 'common/cmdlog/edit',
                    del_url: 'common/cmdlog/del',
                    multi_url: 'common/cmdlog/multi',
                    import_url: 'common/cmdlog/import',
                    table: 'cmd_log',
                }
            });

            var table = $("#table");

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='shop.id']", form).addClass("selectpage").data("source", "/admin/shop/shop/selectpage").data("primaryKey", "id").data("field", "name").data("orderBy", "id desc");
                $("input[name='room.id']", form).addClass("selectpage").data("source", "/admin/shop/shoproom/selectpage").data("primaryKey", "id").data("field", "name").data('params','{"custom[shop_id]":"127"}').data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            $("input[name='room.id']").data("params", function (obj) {
                //obj为SelectPage对象
                console.log('99999999999');
                return {custom: {shop_id: $("input[name='shop.id']").val()}};
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                singleSelect: true,
                searchFormVisible: true,//始终启用高级搜索
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.mobile', title: __('手机号')},
                        {field: 'order_no', title: __('订单号')},
                        {field: 'shop.name', title: __('店铺'), align: 'left', operate: false},
                        {field: 'shop.id', title: __('店铺'), visible: false},
                        {field: 'room.id', title: __('包厢'), visible: false},
                        {
                            field: 'room.name',
                            title: __('包厢'),
                            operate: false,
                            align: 'left',
                            formatter: function (value, row, index, field) {
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + value + "'>" + value + "</span>";
                            },
                            cellStyle: function () {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "180px"
                                    }
                                };
                            }
                        },
                        {
                            field: 'cmd',
                            title: __('指令内容'),
                            align: 'left',
                            operate: false,
                            formatter: function (value, row, index, field) {
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + value + "'>" + value + "</span>";
                            },
                            cellStyle: function () {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "260px"
                                    }
                                };
                            }
                        },
                        // {
                        //     field: 'admin_id', title: __('搜索店铺'), searchList: function (column) {
                        //         return Template('categorytpl', {});
                        //     }, formatter: function (value, row, index) {
                        //         return '无';
                        //     }, visible: false
                        // },
                        {
                            field: 'create_time',
                            title: __('时间'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons: []
                        }]


                ],
                // queryParams: function (params) {
                //     //这里可以追加搜索条件
                //     var filter = JSON.parse(params.filter);
                //     var op = JSON.parse(params.op);
                //     //这里可以动态赋值，比如从URL中获取admin_id的值，filter.admin_id=Fast.api.query('admin_id');
                //     filter.admin_id = Fast.api.query('admin_id');
                //     op.admin_id = "=";
                //     params.filter = JSON.stringify(filter);
                //     params.op = JSON.stringify(op);
                //     return params;
                // },
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
